<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文本优化</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .container {
            text-align: center;
        }
        .form-group {
            margin-bottom: 20px;
            text-align: left;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        textarea {
            width: 100%;
            min-height: 150px;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
            resize: vertical;
        }
        .button {
            background-color: #4CAF50;
            border: none;
            color: white;
            padding: 15px 32px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
            margin: 4px 2px;
            cursor: pointer;
            border-radius: 4px;
        }
        .select-group {
            margin-bottom: 15px;
        }
        select {
            padding: 8px;
            border-radius: 4px;
            border: 1px solid #ccc;
            width: 100%;
        }
        .result-area {
            margin-top: 30px;
            text-align: left;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>文本优化系统</h1>
        <div class="form-group">
            <label for="textInput">输入文本：</label>
            <textarea id="textInput" placeholder="请输入需要优化的文本..."></textarea>
        </div>
        <div class="form-group">
            <div class="select-group">
                <label for="modelSelect">选择模型：</label>
                <select id="modelSelect">
                    <option value="gpt-3.5-turbo">GPT-3.5 Turbo</option>
                    <option value="gpt-4">GPT-4</option>
                    <option value="gpt-4-turbo">GPT-4 Turbo</option>
                    <option value="qwen-turbo">通义千问</option>
                    <option value="deepseek-chat">DeepSeek Chat</option>
                </select>
            </div>
            <div class="select-group">
                <label for="styleSelect">优化风格：</label>
                <select id="styleSelect">
                    <option value="formal">正式风格</option>
                    <option value="casual">日常风格</option>
                    <option value="academic">学术风格</option>
                    <option value="business">商务风格</option>
                    <option value="creative">创意风格</option>
                </select>
            </div>
            <div class="form-group">
                <label for="instructionInput">自定义指令：</label>
                <textarea id="instructionInput" placeholder="请输入自定义优化指令，例如：将文本改写成正式的商务邮件格式..." rows="3"></textarea>
            </div>
        </div>
        <button id="optimizeBtn" class="button">优化文本</button>
        
        <div id="resultArea" class="result-area" style="display: none;">
            <h2>优化结果</h2>
            <textarea id="optimizedText" readonly rows="15"></textarea>
        </div>
    </div>

    <script>
        document.getElementById('optimizeBtn').addEventListener('click', async function() {
            const text = document.getElementById('textInput').value.trim();
            const model = document.getElementById('modelSelect').value;
            const style = document.getElementById('styleSelect').value;
            const instruction = document.getElementById('instructionInput').value.trim();
            
            if (!text) {
                alert('请输入需要优化的文本');
                return;
            }
            
            // 禁用按钮
            this.disabled = true;
            this.textContent = '正在优化...';
            
            try {
                const response = await fetch('/api/optimize', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        text: text,
                        model: model,
                        style: style,
                        instruction: instruction
                    })
                });
                
                const data = await response.json();
                
                if (data.optimized_text) {
                    document.getElementById('optimizedText').value = data.optimized_text;
                    document.getElementById('resultArea').style.display = 'block';
                } else {
                    alert('优化失败: ' + (data.error || '未知错误'));
                }
            } catch (error) {
                console.error('Error:', error);
                alert('发生错误，请稍后重试');
            } finally {
                // 重新启用按钮
                this.disabled = false;
                this.textContent = '优化文本';
            }
        });
    </script>
</body>
</html> 